package com.moli.iov.admin.web.controller;

import com.moli.iov.admin.model.vo.req.CompanyVo;
import com.moli.iov.admin.service.ICompanyFacadeService;
import com.moli.iov.model.dto.req.PageExtend;
import com.moli.iov.rest.RestResponse;
import com.moli.iov.sys.model.po.Company;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 企业管理 控制器
 * </p>
 *
 * @author zql
 * @since 2019/9/4 15:58
 */
@Slf4j
@RestController
@RequestMapping("/sys")
public class CompanyController extends BaseController {

  @Autowired
  private ICompanyFacadeService companyFacadeService;

  /**
   *
   * 分页查询企业列表
   *
   * @param companyVo
   * @return
   */
  @RequestMapping("/company/list")
  public RestResponse<PageExtend<Company>> listCompanyOfPage(
      @RequestBody(required = false) CompanyVo companyVo) {

    if(companyVo == null){
      companyVo = new CompanyVo();
    }
    assembleUserInfo(companyVo);
    return companyFacadeService.listCompanyOfPage(companyVo);
  }

  /**
   *
   * 查询企业列表
   *
   * @return
   */
  @RequestMapping("/company/listCompany")
  public RestResponse<List<Company>> listCompany(
      @RequestBody(required = false) CompanyVo companyVo){
    if(companyVo == null){
      companyVo = new CompanyVo();
    }

    assembleUserInfo(companyVo);
    return companyFacadeService.listCompany(companyVo);
  }

  /**
   * 增加企业
   * @param companyVo
   * @return
   */
  @RequestMapping("/company/add")
  public RestResponse saveCompany(
      @RequestBody @Validated({CompanyVo.Save.class}) CompanyVo companyVo) {
    assembleUserInfo(companyVo);
    log.info("增加企业 入参：{}", companyVo);
    return companyFacadeService.saveCompany(companyVo);
  }

  /**
   * 修改企业
   * @param companyVo
   * @return
   */
  @RequestMapping("/company/update")
  public RestResponse updateCompanyByCompanyId(
      @RequestBody @Validated({CompanyVo.Update.class})CompanyVo companyVo) {
    assembleUserInfo(companyVo);
    companyVo.setEmail(null);
    return companyFacadeService.updateCompanyByCompanyId(companyVo);
  }

  /**
   * 获取企业
   * @param companyVo
   * @return
   */
  @RequestMapping("/company/get")
  public RestResponse<Company> getCompanyByCompanyId(
      @RequestBody @Validated({CompanyVo.Get.class}) CompanyVo companyVo){
    assembleUserInfo(companyVo);
    return companyFacadeService.getCompanyByCompanyId(companyVo);
  }

  /**
   * 导出
   */
  @RequestMapping("/company/export")
  public void export(@RequestBody(required = false) CompanyVo companyVo,
      HttpServletRequest req, final HttpServletResponse res){
    try{
      if(companyVo == null){
        companyVo = new CompanyVo();
      }
      assembleUserInfo(companyVo);
      companyFacadeService.exportCompany(companyVo, req, res);
    }catch(Exception e){
      log.error("企业导出 异常", e);
    }
  }
}
